/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
 
 
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BarcodePDF417;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
 
public class fact {
	class HPdfContentByte extends PdfContentByte {

		public HPdfContentByte(PdfWriter wr) {
			super(wr);
			// TODO Auto-generated constructor stub
		}
	}

	float maxX = 216;
	float maxY = 279;

	float xini  = 15;
	float yini  = 17;
	float w     = 176;								//216-40;
	float h     = 237;								//279-42;
	float dx    = 2;
	float dy    = 2;
	float imgw  = 40; //36;
	float imgh  = 40; //36;
	float xsave = 0;
	
	float xcp   = xini;								//cp:cuadro principal
	float ycp   = yini;
	float wcp   = w;
	float hcp   = h;
	
	float xcre  = xcp + 100 + 15;					//cre:cuadro rut emisor
	float ycre  = ycp + dy;
	float wcre  = xcp + wcp - xcre - dx;
	float hcre  = 30;
	
	float xcdc  = xcp + dx;							//cdc:cuadro datos cliente
	float ycdc  = ycp + dy + imgh + 1;
	float wcdc  = 100;
	float hcdc  = 29;

	float xchd[] = new float[7];
	float wchd[] = new float[7];	
	///////////////////////////////////////////////
	float ychdi = ycdc + hcdc + dy;
	float hchdi = 7;
	
	float xcd   = xcp + dx;							//cd:cuadro detalle
	float ycd   = ychdi + hchdi;
	float wcd   = wcp - 2 * dx;
	float hcd   = 90;
	
	float xcf   = xcp + dx;							//cf:cuadro footer
	float ycf   = ycd + hcd;
	float wcf   = wcd;
	float hcf   = ycp + hcp - ycf - dy;
	
	float xct   = wcp / 2 + 10;						//ct:cuadro totales
	float yct   = ycd + hcd + 11;
	float wct   = xcp + wcp - xct - dx;
	float hct   = 13;

	
	public float T(float coordvalue)
	{
		// transforma de mm a points y transforma coordenadas
		//1pt = 0.3528mm
		//1mm = 2.8346pt
		return coordvalue * (float)2.8346;
	}

	public float X(float x)
	{
		return T(x);
	}
	
	public float Y(float y)
	{
		return T(maxY - y);
	}

	public float W(float w)
	{
		return T(w);
	}

	public float H(float h)
	{
		return T(-h);
	}
	
    /** The resulting PDF. */
    private static final String RESULT = "fact.pdf";
 
    /**
     * Generates a PDF file with different types of barcodes.
     * 
     * @param args
     *            no arguments needed here
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws IOException,
            DocumentException {
        new fact().createPdf(RESULT);
    }
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        //Document document = new Document(new Rectangle(340, 842));
        Document document = new Document(new Rectangle(T(maxX), T(maxY)));
        // step 2
        //PdfWriter.getInstance(document, new FileOutputStream(filename));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        
///*        
        // PDF417
        document.add(new Paragraph("Timbre electrónico"));
        BarcodePDF417 pdf417 = new BarcodePDF417();
        String text = "Call me Ishmael. Some years ago--never mind how long "
            + "precisely --having little or no money in my purse, and nothing "
            + "particular to interest me on shore, I thought I would sail about "
            + "a little and see the watery part of the world.";
        pdf417.setText(text);
        Image img = pdf417.getImage();
        img.scalePercent(50, 50 * pdf417.getYHeight());
        document.add(img);
 //*/
        
        boxes(cb);
        
        // step 5
        document.close();
    }
    
    private void boxes(PdfContentByte c)
    {
    	//save previous line/color settings
    	c.saveState();
    	
    	//cre:cuadro rut emisor
    	c.setLineWidth(T((float)0.7));
        c.setRGBColorStroke(255, 0, 0);
    	c.rectangle(X(xcre), Y(ycre), W(wcre), H(hcre));
        c.stroke();

    	//restore previous line/color settings
        c.restoreState();
        
        //cp:cuadro principal
        c.rectangle(X(xcp), Y(ycp), W(wcp), H(hcp));
    	
    	//cdc:cuadro datos cliente
    	c.rectangle(X(xcdc), Y(ycdc), W(wcdc), H(hcdc));        

    	//chd(i):cuadro header detalle (i) (x 7)
    	xchd[0] = xcp + dx;								//chd(i):cuadro header detalle (i)
    	wchd[0] = 13;
    	xchd[1] = xchd[0] + wchd[0];
    	wchd[1] = 68;
    	xchd[2] = xchd[1] + wchd[1];
    	wchd[2] = 20;
    	xchd[3] = xchd[2] + wchd[2];
    	wchd[3] = 15;
    	xchd[4] = xchd[3] + wchd[3];
    	wchd[4] = 15;
    	xchd[5] = xchd[4] + wchd[4];
    	wchd[5] = 15;
    	wchd[6] = xcp + wcp - (xchd[5] + wchd[5]) - dx;
    	xchd[6] = xchd[5] + wchd[5];	
    	for (int i=0; i <= 6; i++)
    	{
    		c.rectangle(X(xchd[i]), Y(ychdi), W(wchd[i]), H(hchdi));
    	}
    	
    	//cd:cuadro detalle
    	c.rectangle(X(xcd), Y(ycd), W(wcd), H(hcd));
    	
    	//cf:cuadro footer
    	c.rectangle(X(xcf), Y(ycf), W(wcf), H(hcf));
    	
    	//ct:cuadro totales
    	c.rectangle(X(xct), Y(yct), W(wct), H(hct));	    	
    	
        c.stroke();    	
    }
}
